﻿using CommonBehaviors.Actions;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;
using Action = Styx.TreeSharp.Action;

using P = Priest.PriestSettings;
using PD = Priest.PriestSettingsDisc;
using HKM = Priest.PriestHotkeyManagers;

namespace Priest
{
    public partial class PriestMain : CombatRoutine
    {
        public const string
            //common
            BLOODLUST = "Bloodlust",
            HEROISM = "Heroism",
            TIME_WARP = "Time Warp",
            ANCIENT_HYSTERIA = "Ancient Hysteria",
            ENHANCED_AGILITY = "Enhanced Agility",
            ENHANCED_STRENGHT = "Enhanced Strenght",
            ENHANCED_INTELLECT = "Enhanced Intellect",
            //stamina buffs
            _BloodPact = "Blood Pact",
            _CommandingShout = "Commanding Shout",
            _LoneWolfFortitudeoftheBear = "Lone Wolf: Fortitude of the Bear",
            _InvigoratingRoar = "Invigorating Roar",
            _Sturdiness = "Sturdiness",
            _SavageVigor = "Savage Vigor",
            _QirajiFortitude = "Qiraji Fortitude",
            _PowerWordFortitude = "Power Word: Fortitude",


            //priest
            ARCHANGEL = "Archangel",
            SHADOWFORM = "Shadowform",
            SWP = "Shadow Word: Pain",
            SWD = "Shadow Word: Death",
            SMITE = "Smite",
            MIND_FLAY = "Mind Flay",
            MIND_BLAST = "Mind Blast",
            MIND_SPIKE = "Mind Spike",
            DEVOURING_PLAGUE = "Devouring Plague",
            VAMPIRIC_TOUCH = "Vampiric Touch",
            MIND_SEAR = "Mind Sear",
            PWF = "Power Word: Fortitude",
            PWS = "Power Word: Shield",
            RENEW = "Renew",
            SILENCE = "Silence",
            FLASH_HEAL = "Flash Heal",
            SHADOWFIEND = "Shadowfiend",
            MINDBENDER = "Mindbender",
            GIFT_OF_THE_NAARU = "Gift of the Naaru",
            HALO = "Halo",
            CASCADE = "Cascade",
            DIVINE_STAR = "Divine Star",
            POWER_INFUSION = "Power Infusion",
            VAMPIRIC_EMBRACE = "Vampiric Embrace",
            VOID_TENDRILS = "Void Tendrils",
            PSYCHIC_SCREAM = "Psychic Scream",
            DISPEL_MAGIC = "Dispel Magic",
            CLARITY_OF_POWER = "Clarity of Power",
            VOID_ENTROPY = "Void Entropy",
            AUSPICIOUS_SPIRITS = "Auspicious Spirits",
            INSANITY = "Insanity",
            DISPERSION = "Dispersion",
            DESPERATE_PRAYER = "Desperate Prayer",
            EVANGELISM = "Evangelism",
            TWIST_OF_FATE = "Twist of Fate",
            BINDING_HEAL = "Binding Heal",
            CIRCLE_OF_HEALING = "Circle of Healing",
            PRAYER_OF_MENDING = "Prayer of Mending",
            PRAYER_OF_HEALING = "Prayer of Healing",
            HEAL = "Heal",
            SERENDIPITY = "Serendipity",
            CHACKRA_SERENITY = "Chakra: Serenity",
            CHACKRA_SANCTUARY = "Chakra: Sanctuary",
            HOLY_WORD_SERENITY = "Holy Word: Serenity",
            HOLY_WORD_SANCTUARY = "Holy Word: Sanctuary",
            GUARDIAN_SPIRIT = "Guardian Spirit",
            LIGHTWELL = "Lightwell",
            PURIFY = "Purify",
            DIVINE_HYMN = "Divine Hymn",
            WORDS_OF_MENDING = "Words of Mending",
            GUARDIAN_OF_SPIRIT = "Guardian of Spirit",
            POWER_WORD_SOLACE = "Power Word: Solace",
            POWER_WORD_BARRIER = "Power Word: Barrier",
            PAIN_SUPPRESSION = "Pain Suppression",
            PENANCE = "Penance",
            HOLY_FIRE = "Holy Fire",
            HOLY_NOVA = "Holy Nova",
            EINDE = "The End";


        public const int
            ALCHEMYFLASK_ITEM = 75525,
            CRYSTAL_OF_INSANITY_ITEM = 86569,
            CRYSTAL_OF_INSANITY_BUFF = 127230,
            HEALTHSTONE_ITEM = 5512,
            CRYSTAL_OF_ORALIUS_BUFF = 176151,
            CRYSTAL_OF_ORALIUS_ITEM = 118922,
            MIND_SPIKE_INT = 73510,
            TELAARI_TALBUK_INT = 165803,

            EVANGELISM_INT = 81661,
            ARCHANGEL_INT = 81700,
            WEAKENED_SOUL_INT = 6788,
            SERENDIPITY_INT = 63733,
            WORDS_OF_MENDING_INT = 155362,
            PRAYER_OF_MENDING_INT = 33076,
            INSANITY_INT = 132573,
            TWIST_OF_FATE_INT = 123254,
            EIND = 0;


        public static bool needArchangel()
        {
            if (!spellOnCooldown(ARCHANGEL) && IsOverlayed(ARCHANGEL_INT)) return true;
            if (!spellOnCooldown(ARCHANGEL) && buffExists(EVANGELISM_INT, Me) && buffStackCount(EVANGELISM_INT, Me) >= PD.myPrefs.ArchangelStacks) return true;
            return false;
        }

        public static HashSet<string> staminaBuffs = new HashSet<string>
        {
            "Blood Pact",
            "Commanding Shout",
            "Lone Wolf: Fortitude of the Bear",
            "Invigorating Roar",
            "Sturdiness",
            "Savage Vigor",
            "Qiraji Fortitude",
            "Power Word: Fortitude"
        };
        public static bool needPWF
        {
            get
            {
                /*foreach (string aura in staminaBuffs)
                {
                    Logging.Write("Aura: " + aura);
                }*/
                if (buffExists("Blood Pact", Me)) return false;
                if (buffExists("Commanding Shout", Me)) return false;
                if (buffExists("Lone Wolf: Fortitude of the Bear", Me)) return false;
                if (buffExists("Invigorating Roar", Me)) return false;
                if (buffExists("Sturdiness", Me)) return false;
                if (buffExists("Savage Vigor", Me)) return false;
                if (buffExists("Qiraji Fortitude", Me)) return false;
                if (buffExists("Power Word: Fortitude", Me)) return false;
                return true;
            }
        }
        public static string LSPELLCAST = string.Empty;

        #region shadowfiend
        public static string Fiend
        {
            get { return SpellManager.HasSpell(MINDBENDER) ? MINDBENDER : SHADOWFIEND; }
        }

        public static bool FiendConditions
        {
            get
            {
                if (spellOnCooldown(Fiend)) return false;
                if (!P.myPrefs.FiendAsDps) return false;
                if (!P.myPrefs.FiendAsMana) return false;
                if (P.myPrefs.FiendAsMana && Me.ManaPercent <= P.myPrefs.PercentFiendMana) return true;
                if (P.myPrefs.FiendAsDps && HKM.cooldownsOn || (AutoBot && Targets.IsWoWBoss(Me.CurrentTarget))) return true;
                return false;
            }
        }
        #endregion

        #region trinkets
        public static bool UseTrinket1
        {
            get
            {
                if (P.myPrefs.Trinket1Use) return false;
                if (P.myPrefs.Trinket1 == 1) return false;
                if (P.myPrefs.Trinket1 == 2 && (HKM.cooldownsOn || (Targets.IsWoWBoss(Me.CurrentTarget) && AutoBot))) return true;
                if (P.myPrefs.Trinket1 == 3 && IsCrowdControlledPlayer(Me)) return true;
                if (P.myPrefs.Trinket1 == 4 && Me.EnergyPercent <= P.myPrefs.PercentTrinket1Energy) return true;
                if (P.myPrefs.Trinket1 == 5 && Me.ManaPercent >= P.myPrefs.PercentTrinket1Mana) return true;
                if (P.myPrefs.Trinket1 == 6 && Me.HealthPercent <= P.myPrefs.PercentTrinket1HP) return true;
                return false;
            }
        }
        public static bool UseTrinket2
        {
            get
            {
                if (P.myPrefs.Trinket2Use) return false;
                if (P.myPrefs.Trinket2 == 1) return false;
                if (P.myPrefs.Trinket2 == 2 && (HKM.cooldownsOn || (Targets.IsWoWBoss(Me.CurrentTarget) && AutoBot)))
                    if (P.myPrefs.Trinket2 == 3 && IsCrowdControlledPlayer(Me)) return true;
                if (P.myPrefs.Trinket2 == 4 && Me.EnergyPercent <= P.myPrefs.PercentTrinket2Energy) return true;
                if (P.myPrefs.Trinket2 == 5 && Me.ManaPercent >= P.myPrefs.PercentTrinket2Mana) return true;
                if (P.myPrefs.Trinket2 == 6 && Me.HealthPercent <= P.myPrefs.PercentTrinket2HP) return true;
                return false;
            }
        }
        private static bool CanUseEquippedItem(WoWItem item)
        {
            string itemSpell = Lua.GetReturnVal<string>("return GetItemSpell(" + item.Entry + ")", 0);
            if (string.IsNullOrEmpty(itemSpell))
                return false;
            return item.Usable && item.Cooldown <= 0;
        }
        #endregion

        #region hastebuffs
        public static bool HaveHasteBuff
        {
            get
            {
                return Me.HasAura(BLOODLUST)
                    || Me.HasAura(HEROISM)
                    || Me.HasAura(TIME_WARP)
                    || Me.HasAura("Haste")
                    || Me.HasAura("Berserking")
                    || Me.HasAura(ANCIENT_HYSTERIA);
            }
        }
        #endregion

        #region buff condtions

        public static bool AlchemyFlaskConditions
        {
            get
            {
                return !Me.HasAura(ENHANCED_AGILITY)
                    && !Me.HasAura(ENHANCED_INTELLECT)
                    && !Me.HasAura(ENHANCED_STRENGHT)
                    && !Me.HasAura(CRYSTAL_OF_INSANITY_BUFF)
                    && !Me.HasAura(CRYSTAL_OF_ORALIUS_BUFF);
            }
        }
        public static bool CrystalOfOraliusConditions
        {
            get { return !Me.HasAura(CRYSTAL_OF_ORALIUS_BUFF); }
        }
        public static bool CrystalOfInsanityConditions
        {
            get { return !Me.HasAura(CRYSTAL_OF_ORALIUS_BUFF) && !Me.HasAura(CRYSTAL_OF_INSANITY_BUFF); }
        }

        #endregion

        #region interrupts
        public static DateTime interruptTimer;
        public static bool SilenceConditions(WoWUnit unit)
        {
            if (P.myPrefs.AutoInterrupt
                && unit.IsCasting
                && Me.CanInterruptCurrentSpellCast
                && !spellOnCooldown(SILENCE)
                && interruptTimer <= DateTime.Now)
            {
                interruptTimer.AddSeconds(5);
                return true;
            }
            return false;
        }
        #endregion

        #region overlayed
        public static bool IsOverlayed(int spellID)
        {
            return Lua.GetReturnVal<bool>("return IsSpellOverlayed(" + spellID + ")", 0);
        }
        #endregion

        public static bool MindSpikeConditions
        {
            get
            {
                if (IsOverlayed(MIND_SPIKE_INT)) { return true; }
                if (SpellManager.HasSpell(TWIST_OF_FATE) && SpellManager.HasSpell(CLARITY_OF_POWER) && Me.CurrentTarget.HealthPercent <= 36)
                {
                    return true;
                }
                return false;
            }
        }
        public static bool vtConditions(WoWUnit unit)
        {
            if (buffExists(TWIST_OF_FATE_INT, Me)) return false;
            if (addCountRange >= P.myPrefs.MindSearAdds) return false;
            if ((!debuffExists(VAMPIRIC_TOUCH, unit)
            || (debuffExists(VAMPIRIC_TOUCH, unit) && debuffTimeLeft(VAMPIRIC_TOUCH, unit) <= 4500))) return true;
            return false;
        }
        public static bool swpConditions(WoWUnit unit)
        {
            if (buffExists(TWIST_OF_FATE_INT, Me)) return false;
            if (addCountRange >= P.myPrefs.MindSearAdds) return false;
            if ((!debuffExists(SWP, unit)
                || (debuffExists(SWP, unit) && debuffTimeLeft(SWP, unit) <= 4500))) return true;
            return false;
        }
    }
}
